package org.jeecg.modules.user.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.user.entity.WlhyUserBank;
import org.jeecg.modules.user.service.IWlhyUserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.jeecg.common.constant.WlhyBizConstant.ROW_STATUS1;

/**
 * @Description: 用户银行卡管理
 * @Author: jeecg-boot
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Api(tags = "用户银行卡管理")
@RestController
@RequestMapping("/user-api/user/wlhyUserBank")
@Slf4j
public class WlhyUserBankApi extends JeecgController<WlhyUserBank, IWlhyUserBankService> {
    @Autowired
    private IWlhyUserBankService wlhyUserBankService;

    /**
     * 分页列表查询
     *
     * @return
     */
    //@AutoLog(value = "用户银行卡管理-分页列表查询")
    @ApiOperation(value = "用户银行卡管理-分页列表查询", notes = "用户银行卡管理-分页列表查询")
    @GetMapping(value = "/myList")
    public Result<List<WlhyUserBank>> queryPageList() {

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        List<WlhyUserBank> pageList = wlhyUserBankService.list(new LambdaQueryWrapper<WlhyUserBank>()
                .eq(WlhyUserBank::getUserId, loginUser.getId())
                .eq(WlhyUserBank::getStatus, ROW_STATUS1)
                .orderByDesc(WlhyUserBank::getIzDefault)
                .orderByDesc(WlhyUserBank::getUpdateTime)
        );
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param wlhyUserBank
     * @return
     */
    @AutoLog(value = "用户银行卡管理-添加")
    @ApiOperation(value = "用户银行卡管理-添加", notes = "用户银行卡管理-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody @Validated WlhyUserBank wlhyUserBank) {
        wlhyUserBankService.add(wlhyUserBank);
        return Result.OK("添加成功！");
    }

    /**
     * 设为默认
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户银行卡管理-设为默认")
    @ApiOperation(value = "用户银行卡管理-设为默认", notes = "用户银行卡管理-设为默认")
    @PostMapping(value = "/setDefault")
    public Result<String> setDefault(@RequestParam String id) {
        wlhyUserBankService.setDefault(id);
        return Result.OK("操作成功！");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户银行卡管理-通过id删除")
    @ApiOperation(value = "用户银行卡管理-通过id删除", notes = "用户银行卡管理-通过id删除")
    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wlhyUserBankService.delete(id);
        return Result.OK("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户银行卡管理-通过id查询")
    @ApiOperation(value = "用户银行卡管理-通过id查询", notes = "用户银行卡管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WlhyUserBank> queryById(@RequestParam(name = "id", required = true) String id) {
        WlhyUserBank wlhyUserBank = wlhyUserBankService.getById(id);
        if (wlhyUserBank == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wlhyUserBank);
    }

 /**
     * 获取默认银行卡
     *
     * @return
     */
    //@AutoLog(value = "用户银行卡管理-获取默认银行卡")
    @ApiOperation(value = "用户银行卡管理-获取默认银行卡", notes = "用户银行卡管理-获取默认银行卡")
    @GetMapping(value = "/myDefaultBank")
    public Result<WlhyUserBank> myDefaultBank() {
        WlhyUserBank wlhyUserBank = wlhyUserBankService.myDefaultBank();
        return Result.OK(wlhyUserBank);
    }


}
